package groupOne.app;

import groupOne.common.Constant;
import groupOne.util.SQLUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author: LixuanFeng
 * @Date: 2022/8/31 0:24
 * @From: GoodGoodStudyDayDayUp
 */
public abstract class BaseAppSql {
    public void init(int port, int p, String ckPathAndJobId) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p);

        env.enableCheckpointing(3000);
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/gmall/" + ckPathAndJobId);
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointTimeout(20 * 1000);
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        // 给sql job起名字
        tEnv.getConfig().getConfiguration().setString("pipeline.name", ckPathAndJobId);


        handle(env, tEnv);
    }

    protected abstract void handle(StreamExecutionEnvironment env, StreamTableEnvironment tEnv);

    // 读取ods_db数据
    public void readOdsDb(StreamTableEnvironment tEnv,String groupId){
        tEnv.executeSql("create table ods_db( " +
                " `database` string, " +
                " `table` string, " +
                " `type` string, " +
                " `ts` bigint, " +
                " `data` map<string,string>, " +
                " `old` map<string,string>, " +
                " `pt` as proctime() " +
                ")" + SQLUtil.getKafkaSource(Constant.TOPIC_ODS_DB,groupId));

    }
}